/**
 * 
 */
package com.bs.areport.action.master;

import java.io.InputStream;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.bs.areport.sys.GenCodeImg;
import com.bs.restframework.context.SystemContext;
import com.bs.restframework.context.util.SystemContextUtil;
import com.bs.restframework.session.UserSession;
import com.bs.restframework.util.DesUtils;
import com.bs.restframework.web.result.ResultModle;
import com.bs.restframework.web.result.ResultType;
import com.bs.areport.service.master.UserManageDao;
import com.bs.areport.service.util.EntityType;
import com.bs.areport.service.util.ValiData;

/**
 * 
 * <p>函数名称：用户控制类</p>
 * <p>创建时间：2012-03-08</p>
 * <p>作者：李新宇</p>
 */
public class UserManageAction {
	private UserManageDao dao;
	public  UserManageDao getDao() {
		return dao;
	}

	public void setDao(UserManageDao dao) {
		this.dao = dao;
	}
	
	/**
	 * 
	 * <p>函数名称：isLogin</p>
	 * <p>功能说明：用户是否登录</p>
	 * <p>参数说明：</p>
	 * <p>创建时间：2012-03-08</p>
	 * <p>作者：李新宇</p>
	 */
	public ResultModle isLogin() throws Exception
	{
		Map<String, Object> params = SystemContext.getInParams();
		UserSession userSession = SystemContextUtil.getSessionMange().getUserSession((String)params.get("Token"));
		
		Map<String, Object> resultBean=new HashMap<String, Object>();
		if(userSession!=null)
		    resultBean.put("isLogin", userSession.isLogin());
		else
			resultBean.put("isLogin",false);
		return new ResultModle(ResultType.OUTPUT_JSON_STREAM, "user", resultBean);
	}
	
	/**
	 * 
	 * <p>函数名称：getValidCode</p>
	 * <p>功能说明：获取验证码</p>
	 * <p>参数说明：</p>
	 * <p>创建时间：2012-03-08</p>
	 * <p>作者：李新宇</p>
	 */
	public ResultModle getValidCode() throws Exception
	{
		Map<String, Object> params = SystemContext.getInParams();
		UserSession userSession = SystemContextUtil.getSessionMange().getUserSession((String)params.get("Token"));
		GenCodeImg img = new GenCodeImg();
		InputStream in = img.getImg();
		String validCode=img.getCode();
		userSession.addAttribute("ValidCode", validCode);
        return new ResultModle(ResultType.OUTPUT_STRING_STREAM,in);
	}
	
	/**
	 * 
	 * <p>函数名称：loginout</p>
	 * <p>功能说明：退出</p>
	 * <p>参数说明：</p>
	 * <p>创建时间：2012-03-08</p>
	 * <p>作者：李新宇</p>
	 */
	public ResultModle loginout() throws Exception
	{
		Map<String, Object> params = SystemContext.getInParams();
		UserSession userSession = SystemContextUtil.getSessionMange().getUserSession((String)params.get("Token"));
		if(userSession!=null){
			userSession.setLoginStatus(false);
			userSession.clearAttribute();
			SystemContextUtil.getSessionMange().deleteUserSession(userSession.getSessionId());
		}
        return new ResultModle(ResultType.OUTPUT_JSON_STREAM, "user","");
	}
	/**
	 * 
	 * <p>函数名称：GetSession</p>
	 * <p>功能说明：获取登陆状态</p>
	 * <p>参数说明：</p>
	 * <p>创建时间：2012-04-20</p>
	 * <p>作者：陈金滨</p>
	 */
	public ResultModle getSession() throws Exception
	{
		Map<String, Object> params = SystemContext.getInParams();
		String token =(String)params.get("Token");
		UserSession userSession = SystemContextUtil.getSessionMange().getUserSession((String)params.get("Token"));
		String userID = userSession.getUserID().toString();
		String userType = userSession.getUserType().toString();
		String deptID = userSession.getAttribute("deptID").toString() ;
		Map<String, Object> resultBean=new HashMap<String, Object>();
		resultBean.put("USERID", userID);
		resultBean.put("USERTYPE", userType);
		resultBean.put("DEPTID", deptID);			
		return new ResultModle(ResultType.OUTPUT_JSON_STREAM, "user", resultBean);
	}
	/**
	 * 
	 * <p>函数名称：getToken</p>
	 * <p>功能说明：获取令牌</p>
	 * <p>参数说明：</p>
	 * <p>创建时间：2012-03-08</p>
	 * <p>作者：李新宇</p>
	 */
	public ResultModle getToken() throws Exception
	{
		UserSession userSession = SystemContextUtil.getSystemContext().getUserSession();
		userSession.setUserID(String.valueOf(new Random().nextLong()));
		
		Map<String, Object> resultBean=new HashMap<String, Object>();
		resultBean.put("Token", userSession.getSessionId());
		return new ResultModle(ResultType.OUTPUT_JSON_STREAM, "user", resultBean);
	}
	
	/**
	 * 
	 * <p>函数名称：list</p>
	 * <p>功能说明：查询列表</p>
	 * <p>参数说明：</p>
	 * <p>创建时间：2012-03-08</p>
	 * <p>作者：李新宇</p>
	 */
	public ResultModle list() throws Exception
	{
		Map<String, Object> params = SystemContext.getInParams();
		
        Map<String, Object> resultBean = dao.list(params);
        
		return new ResultModle(ResultType.OUTPUT_JSON_STREAM, "user", resultBean);
	}
	/**
	 * 
	 * <p>函数名称：Qrylist</p>
	 * <p>功能说明：查询列表</p>
	 * <p>参数说明：</p>
	 * <p>创建时间：2012-03-08</p>
	 * <p>作者：李新宇</p>
	 */
	public ResultModle qrylist() throws Exception
	{
		Map<String, Object> params = SystemContext.getInParams();
		
        Map<String, Object> resultBean = dao.qrylist(params);
        
		return new ResultModle(ResultType.OUTPUT_JSON_STREAM, "user", resultBean);
	}	
	
	/**
	 * 
	 * <p>函数名称：login</p>
	 * <p>功能说明：登录</p>
	 * <p>参数说明：</p>
	 * <p>创建时间：2012-03-08</p>
	 * <p>作者：李新宇</p>
	 */
	public ResultModle login() throws Exception
	{
		Map<String, Object> resultBean =new HashMap<String, Object>();
		Map<String, Object> params = SystemContext.getInParams();
		String msg="";
		UserSession userSession = SystemContextUtil.getSessionMange().getUserSession((String)params.get("Token"));
		if(userSession==null){
			msg="网页已过期，请刷新页面!";
		}else{
			String code=(String)params.get("F_CODE");
			String pwd=(String)params.get("F_PASSWORD");
			String validCode="";
			if(userSession.getAttribute("ValidCode")!=null)
			    validCode=userSession.getAttribute("ValidCode").toString();
			
			Map<String, Object> loginInfo=null;
			if(code==null||"".equals(code))
				msg="用户不能为空!";
			else if(pwd==null||"".equals(pwd))
				msg="密码不能为空!";
			//不检查验证码
			//		else if(!validCode.equals((String)params.get("validCode")))
			//			msg="验证码不正确";
			else{
				loginInfo=dao.login(code, pwd);
				msg=(String)loginInfo.get("msg");
			}
			
			if("".equals(msg)){
				userSession.setLoginStatus(true);
				userSession.setUserID((String)((Map)loginInfo.get("user")).get("F_ID"));
				userSession.setUserName((String)((Map)loginInfo.get("user")).get("F_NAME"));
				userSession.setUserType((String)((Map)loginInfo.get("user")).get("F_USERTYPE"));
				userSession.addAttribute("lastTime", (String)((Map)loginInfo.get("user")).get("F_LASTTIME"));
				userSession.addAttribute("deptID", (String)((Map)loginInfo.get("user")).get("F_DEPTID"));
				userSession.addAttribute("deptCode", (String)((Map)loginInfo.get("user")).get("DEPTCODE"));
				userSession.addAttribute("right", (List<Map>)loginInfo.get("right"));
				resultBean.put("Token",(String)params.get("Token"));
			}
		}
		resultBean.put("msg", msg);
		return new ResultModle(ResultType.OUTPUT_JSON_STREAM, "user", resultBean);
	}
	
	/**
	 * 
	 * <p>函数名称：home</p>
	 * <p>功能说明：加载主界面</p>
	 * <p>参数说明：</p>
	 * <p>创建时间：2012-03-08</p>
	 * <p>作者：李新宇</p>
	 */
	public ResultModle home() throws Exception
	{
		Map<String, Object> params = SystemContext.getInParams();
		Map<String, Object> resultBean =new HashMap<String, Object>();
		UserSession userSession = SystemContextUtil.getSessionMange().getUserSession((String)params.get("Token"));
		
		if(userSession.isLogin()){
			resultBean.put("userName", userSession.getUserName());
			resultBean.put("deptID", userSession.getAttribute("deptID"));
			resultBean.put("right", userSession.getAttribute("right"));
			resultBean.put("lastTime", userSession.getAttribute("lastTime"));
		}
		
		//取服务器时间
		
	    Calendar now = Calendar.getInstance();
	    String time = now.get((Calendar.YEAR)) + "";
	    time += "年";
	    time += now.get(Calendar.MONTH) + 1;
	    time += "月";
	    time += now.get(Calendar.DATE);
	    time += "日";

	    String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };

	    int w = now.get(Calendar.DAY_OF_WEEK) - 1;
	    if (w < 0)
	      w = 0;
	    
	    resultBean.put("date",time + " " + weekDays[w]); 
		resultBean.put("Token",params.get("Token"));
		return new ResultModle(ResultType.OUTPUT_JSP_VIEW, "/pages/home/master/home", resultBean);
	}
	
	/**
	 * 
	 * <p>函数名称：getMaxCode</p>
	 * <p>功能说明：获取最大编码值/p>
	 * <p>参数说明：</p>
	 * <p>创建时间：2012-03-08</p>
	 * <p>作者：李新宇</p>
	 * <p>修改：陈金滨  2012-03-22 最大编码加1</p>
	 */
	public ResultModle getMaxCode() throws Exception
	{				
		Map<String, Object> params = SystemContext.getInParams();
		String token =(String)params.get("Token");
		UserSession userSession = SystemContextUtil.getSessionMange().getUserSession((String)params.get("Token"));
		String userid=userSession.getUserID().toString() ;
		Map<String, Object> userInfo = dao.getInfoByID(userid);		
		String userType = userSession.getUserType().toString() ;		
		String newCode = "" ;
		Map<String, Object> resultBean= new HashMap<String, Object>();
		if("A".equals(userType)==false)
		{
			String DeptCode = userSession.getAttribute("deptCode").toString() ;
			String deptID = userInfo.get("F_DEPTID").toString() ;
			resultBean=dao.getMaxCode(DeptCode,deptID);
			String MaxCode=(String)resultBean.get("F_CODE");
			int codelen = MaxCode.length() ;//最大编码长度
			int deptCodeLen = DeptCode.length() ;
			String codeRight = MaxCode.substring(deptCodeLen, codelen).toString();
			
			//判断编码长度是否大于9，大于9则拆分为两段
			int codeLen = codeRight.length();
			if(codeLen<=9)
			{
				
				int oldcode = Integer.valueOf(codeRight).intValue();
				oldcode = oldcode + 1 ;
				newCode = String.valueOf(oldcode).toString();
				int codelen2 = newCode.length();
				if(codelen2<codeLen)
				{
					for(int i=0;i<(codeLen-codelen2);i++)
					{
						newCode = "0"+newCode ;
					}			
				}
				newCode = DeptCode + newCode ;
			}else{
				
				String codeRright = codeRight.substring(9, codeLen).toString();
				String codeLeft = MaxCode.substring(0, deptCodeLen+9).toString();
				int codeRrightLen = Integer.valueOf(codeRright).intValue();
				codeRrightLen = codeRrightLen + 1 ;
				newCode = String.valueOf(codeRrightLen).toString();
				newCode = codeLeft+newCode;
				
			}	
			
						
		}
		resultBean.put("F_CODE", newCode);			
		return new ResultModle(ResultType.OUTPUT_JSON_STREAM, "user", resultBean);
	}
	
	/**
	 * 
	 * <p>函数名称：edit</p>
	 * <p>功能说明：编辑</p>
	 * <p>参数说明：</p>
	 * <p>创建时间：2012-03-08</p>
	 * <p>作者：李新宇</p>
	 * <p>修改：陈金滨 增加最后修改时间</p>
	 * @throws Exception 
	 */
	public ResultModle edit() throws Exception{
		Map<String, Object> params = SystemContext.getInParams();
		String userID = (String) params.get("F_ID");
		Map<String, Object> Info = dao.getInfoByID(userID) ;  
		
		String token =(String)params.get("Token");
		UserSession userSession = SystemContextUtil.getSessionMange().getUserSession((String)params.get("Token"));
		String userid=userSession.getUserID().toString() ;
		params.put("F_USER", userid);
		
		boolean tag = false;
		String msg = "";
		
		String nDeptid = (String) params.get("F_DEPTID");
		String oDeptid = (String) Info.get("F_DEPTID");
		if(nDeptid==null||"".equals(nDeptid))
		{
			params.put("F_DEPTID", oDeptid) ;	
		}
		if(nDeptid==null||"所有".equals(nDeptid))
		{
			params.put("F_DEPTID", "") ;	
		}
		
		String nUsergroupid = (String) params.get("F_USERGROUPID");
		String oUsergroupid = (String) Info.get("F_USERGROUPID");
		if(nUsergroupid==null||"".equals(nUsergroupid))
		{
			if("999".equals(nUsergroupid)==false)
			{
				params.put("F_USERGROUPID", oUsergroupid) ;
			}else{
				
			}	params.put("F_USERGROUPID", "") ;			
		}
		
		String nUserType = (String) params.get("F_USERTYPE");		
		String oUserType = (String) Info.get("F_USERTYPE");
		if(nUserType==null||"".equals(nUserType))
		{
			params.put("F_USERTYPE", oUserType) ;	
			nUserType = oUserType;
		}
		
		String nPhone = (String) params.get("F_PHONENO");
		String oPhone = (String) Info.get("F_PHONENO");
		if(nPhone==null||"".equals(nPhone))
		{
			params.put("F_PHONENO", oPhone) ;	
		}	
		
		params.remove("F_PASSWORD");
        Date currentTime = new Date();
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmsssss");
	    String dateString = formatter.format(currentTime);		
	    params.put("F_LASTTIME", dateString);		
	    
	    String code = (String) params.get("F_CODE"); 
	    String checkInfo = this.checkCode(code, userID);  //验证编码是否符合要求
	    
	    String deptID = (String) params.get("F_DEPTID") ;//新增的用户单位id
		Map<String, Object> userMap = dao.getInfoByID(userid); //当前登陆用户信息
		String userDeptID = ""; //当前登录用户单位ID
		if(userMap!=null)
		{
			userDeptID = (String) userMap.get("F_DEPTID");
			if(userDeptID==null)
				userDeptID="";
			params.put("F_HIGHTUP", userDeptID);
		}
	    if("G".equals(nUserType))
		{
			if(deptID.equals(userDeptID))
			{
				msg = "不能创建同级管理员!";
			}
			else
			{
				//增加一个单位只能有一个管理员    陈金滨 2012-05-09
			    boolean isManager = dao.isGmExist(deptID,userID);		    
				if(isManager)
				{
					msg = "该单位已存在管理员!" ;
				}else
				{
					msg = "" ;
				}				
			}

				
		}
	    if("P".equals(nUserType))
	    {
	    	if(deptID.equals(userDeptID)==false)
			{
				msg = "不能为下级创建一般用户!";
			}
	    }
	  
		try
		{   
//			msg=ValiData.valiEntity(params, EntityType.SYS_USER);
			if ("".equals(msg))
			{
				if("".equals(checkInfo)) //判断编码是否正确，是否重复
				{
					msg = dao.edit(params);
					tag = true;
					msg = "修改成功!";	
				}
				else
				{
					msg = checkInfo;
				}
			    
			}
		}
		catch(Exception e)
		{
			msg = "修改失败" + e;
		}
        
		Map<String, Object> resultBean=new HashMap<String, Object>();
		resultBean.put("msg", msg);
		resultBean.put("tag", tag);
		return new ResultModle(ResultType.OUTPUT_JSON_STREAM, "user", resultBean);

	}
	/**
	 * 
	 * <p>函数名称：editState</p>
	 * <p>功能说明：更改使用状态</p>
	 * <p>参数说明：</p>
	 * <p>创建时间：2012-03-28</p>
	 * <p>作者：陈金滨</p>
	 * @throws Exception 
	 */
	public ResultModle editState() throws Exception{
		Map<String, Object> params = SystemContext.getInParams();
		String id = (String) params.get("F_ID");
		String state = (String)params.get("F_STATE");
		String token =(String)params.get("Token");
		UserSession userSession = SystemContextUtil.getSessionMange().getUserSession((String)params.get("Token"));
		String user=userSession.getUserID().toString() ;
		boolean tag = false;
		String msg = "";
        Date currentTime = new Date();
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmsssss");
	    String dateString = formatter.format(currentTime);		
	    params.put("F_LASTTIME", dateString);		
	    boolean isManager = false ;
	    Map<String, Object> vMap = dao.getInfoByID(id);
	    if(vMap!=null&&"".equals(vMap)==false)
	    {
	    	String userType = (String) vMap.get("F_USERTYPE");
	    	String deptID = (String) vMap.get("F_DEPTID");
	    	if("G".equals(userType))
	    	{
	    		isManager = dao.isGmExist(deptID,id);
	    	}
	    }
	    
		try
		{   	
			if((state.equals("1")&&(id.equals(user))))
			{
				msg = "不能停用当前登录用户!";
			}
			else
			{
				if(isManager)
				{
					msg = "已存在启用状态的管理员,不能同时启用两个!" ;
				}					
				if("".equals(msg)) 
				{				
					msg = dao.edit(params);
					tag = true;
					if("1".equals(state))
					{
						msg = "停用成功!";	
					}
					if("0".equals(state))
					{
						msg = "启用成功!" ;
					}
							
				}		
			}				
		}
		catch(Exception e)
		{
			msg = "操作失败" + e;
		}
        
		Map<String, Object> resultBean=new HashMap<String, Object>();
		resultBean.put("msg", msg);
		resultBean.put("tag", tag);
		return new ResultModle(ResultType.OUTPUT_JSON_STREAM, "user", resultBean);

	}	
	/**
	 * 
	 * <p>函数名称：del</p>
	 * <p>功能说明：删除</p>
	 * <p>参数说明：</p>
	 * <p>创建时间：2012-03-08</p>
	 * <p>作者：李新宇</p>
	 */
	public ResultModle del() throws Exception{
		Map<String, Object> params = SystemContext.getInParams(); 
		String id = (String) params.get("F_ID");
		String token =(String)params.get("Token");
		UserSession userSession = SystemContextUtil.getSessionMange().getUserSession((String)params.get("Token"));
		String user=userSession.getUserID().toString() ;
		boolean tag=false;
		String msg =""; 
		try
		{
			if(id.equals(user)==false)
			{
				msg = dao.del(params);
				if("".equals(msg))
				{
					tag = true;
					msg = "删除成功!";
				}
				else
				{
					msg = "删除失败!";
				}	
			}
			else
			{
				msg ="不能删除当前登录用户!" ;
			}
			
		}
		catch(Exception e)
		{
			msg = "删除失败!" + e;
		}
		
		Map<String, Object> resultBean=new HashMap<String, Object>();
		resultBean.put("msg", msg);
		resultBean.put("tag", tag);
		return new ResultModle(ResultType.OUTPUT_JSON_STREAM, "user", resultBean);

	}
	
	/**
	 * 
	 * <p>函数名称：add</p>
	 * <p>功能说明：添加</p>
	 * <p>参数说明：</p>
	 * <p>创建时间：2012-03-08</p>
	 * <p>作者：李新宇</p>
	 * <p>修改：陈金滨 增加最后修改时间</p>
	 * @throws Exception 
	 */
	public ResultModle add() throws Exception{
		Map<String, Object> params = SystemContext.getInParams();
		System.out.println(params);
		boolean tag = false;
		String msg = "";
		//最后修改时间  陈金滨  2012.04.25
        Date currentTime = new Date();
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmsssss");
	    String dateString = formatter.format(currentTime);		
	    params.put("F_LASTTIME", dateString);
	    params.put("F_STATE", "0");
  
	    String editCode = (String) params.get("F_CODE"); 
	    @SuppressWarnings("unused")
		String editName = (String) params.get("F_NAME"); 
	    String token =(String)params.get("Token");
		UserSession userSession = SystemContextUtil.getSessionMange().getUserSession((String)params.get("Token"));
		String userid=userSession.getUserID().toString() ;
		
		params.put("F_USER", userid);
		String userType = (String) params.get("F_USERTYPE"); //新增用户类型
		String deptID = (String) params.get("F_DEPTID") ;//新增用户单位
		Map<String, Object> loadUserInfo = dao.getInfoByID(userid);
		String loadUserDept = ""; //当前登录用户单位		
		if(loadUserInfo!=null)
		{
			loadUserDept = (String) loadUserInfo.get("F_DEPTID");
			if(loadUserDept==null)
				loadUserDept="";
			params.put("F_HIGHTUP", loadUserDept) ; //添加直管上级单位id
		}		
		try
		{						
			if(dao.isCodeExist(editCode))
			{
				msg ="编码已存在，请重新输入!";
			}
			else
			{
				if("G".equals(userType))
				{
					if(deptID.equals(loadUserDept))
					{
						msg = "不能创建同级管理员!" ;
					}else{
						boolean isManager = dao.isGmExist(deptID);
						if(isManager)
							msg = "该单位已存在管理员!" ;
					}
				}
				if("P".equals(userType))
				{
					if(deptID.equals(loadUserDept)==false)
					{
						msg = "不能为下级单位创建一般用户!" ;
					}
				}
					
				if("".equals(msg))
				{   
					//密码加密 李新宇 2012.03.29
					params.put("F_PASSWORD", DesUtils.encryptDSE("666666"));
					msg = dao.add(params);
			
					tag = true;
					msg = "新增成功!";
				}
								
			}	
		}
		catch(Exception e)
		{
			msg = "新增失败" + e;
		}
		Map<String, Object> resultBean=new HashMap<String, Object>();
		resultBean.put("msg", msg);
		resultBean.put("tag", tag);
		return new ResultModle(ResultType.OUTPUT_JSON_STREAM, "user", resultBean);

	}
	/**
	 * 
	 * <p>函数名称：checkCode</p>
	 * <p>功能说明：判断编码是否合法</p>
	 * <p>参数说明：code:编码，id:用户id</p>
	 * <p>创建时间：2012-03-08</p>
	 * <p>作者：陈金滨 </p>
	 * 
	 */
	public String checkCode(String code,String id) throws Exception
	{				
		String msg = "" ;
		if("".equals(id)||id==null)
		{
			msg = "";
		}
		else
		{
			if("".equals(code)||code==null)
			{
				msg ="";
			}
			else
			{
				if(dao.isCodeExist(code))//如果编码存在，继续判断是否是改过的编码
				{
					if(code.equals(dao.getCodeByID(id)))
					{
						msg = "";
					}
					else
					{
						msg ="编码已存在，请重新输入!";
					}
				}
				else
				{
					msg = "";			
				}						
			}	
		}
	    return msg ;
	}	

	/**
	 * 
	 * <p>函数名称：SetPWD</p>
	 * <p>功能说明：设置新密码</p>
	 * <p>参数说明：params：旧密码，新密码，确认新密码</p>
	 * <p>创建时间：2012-03-08</p>
	 * <p>作者：陈金滨 </p>
	 * 
	 */
	public ResultModle setPWD() throws Exception	
	{
		String msg = "";
		Map<String, Object> params = SystemContext.getInParams(); 
		String opwd=(String) params.get("F_PASSWORD");   //原密码
		String npwd=(String) params.get("F_PASSWORD1");  //新密码
		String rpwd=(String) params.get("F_PASSWORD2");  //确认新密码
		String token =(String)params.get("Token");
		UserSession userSession = SystemContextUtil.getSessionMange().getUserSession((String)params.get("Token"));
		String userID=userSession.getUserID().toString() ;
		String pwd = dao.getPwdByID(userID);     //修改前密码
		pwd = DesUtils.decryptDSE(pwd);        //解密
		
		Date currentTime = new Date();
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmsssss");
	    String dateString = formatter.format(currentTime);	
	    
		boolean tag = false ;
		if(opwd.equals(pwd))         //验证原密码是否匹配
		{
			if(npwd.equals(rpwd))     //验证两次输入的新密码是否匹配
			{
				//修改密码
				msg=dao.setPWD(userID, npwd,dateString);
				tag =true ;
			}
			else
			{
				msg = "两次输入的新密码不一致,请重新输入!" ;
//				tag =true ;
			}
		}
		else
		{
			msg = "原密码错误!";
//			tag =true ;
		}
		Map<String, Object> resultBean=new HashMap<String, Object>();
		resultBean.put("msg", msg);		
		resultBean.put("tag", tag);
		return new ResultModle(ResultType.OUTPUT_JSON_STREAM, "user", resultBean);
	}	

	/**
	 * 
	 * <p>函数名称：initUserType</p>
	 * <p>功能说明：单位类型下拉列表</p>
	 * <p>参数说明：</p>
	 * <p>创建时间：2012-03-29</p>
	 * <p>作者：陈金滨</p>
	 */
	public ResultModle initUserType() throws Exception
	{				
		List<Map> resultBean= new ArrayList();
		Map<String, Object> vMap1 = new HashMap<String, Object>();
		vMap1.put("id", "G");
		vMap1.put("text", "管理员");
		resultBean.add(vMap1);
		Map<String, Object> vMap2 = new HashMap<String, Object>();
		vMap2.put("id", "P");
		vMap2.put("text", "一般用户");
		resultBean.add(vMap2);
		return new ResultModle(ResultType.OUTPUT_JSON_STREAM, "user", resultBean);
	}			
	
	/**
	 * 
	 * <p>函数名称：initPwd</p>
	 * <p>功能说明：密码初始化</p>
	 * <p>参数说明：</p>
	 * <p>创建时间：2012-03-08</p>
	 * <p>作者：陈金滨</p>
	 * @throws Exception 
	 */
	public ResultModle initPwd() throws Exception{
		Map<String, Object> params = SystemContext.getInParams();
		String userId = (String) params.get("F_ID");
        params = null ;
        Map<String, Object> newparams = new HashMap<String, Object>();
        newparams.put("F_ID", userId);
        newparams.put("F_PASSWORD",DesUtils.encryptDSE("666666"));
		boolean tag = false;
		String msg = "";
        Date currentTime = new Date();
	    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmsssss");
	    String dateString = formatter.format(currentTime);		
	    newparams.put("F_LASTTIME", dateString);
		try
		{	
			if("".equals(msg))
			{   
				msg = dao.initPwd(newparams);
		
				tag = true;
				msg = "初始化密码成功!";
			}		
		}
		catch(Exception e)
		{
			msg = "初始化密码失败" + e;
		}
		Map<String, Object> resultBean=new HashMap<String, Object>();
		resultBean.put("msg", msg);
		resultBean.put("tag", tag);
		return new ResultModle(ResultType.OUTPUT_JSON_STREAM, "user", resultBean);

	}	
}
